package com.huqiwen.table;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JgridTest extends HttpServlet {
	private static final long serialVersionUID = 132383828833L; 

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		JSONObject json = new JSONObject();
		json.put("page", 1);
		json.put("total", 1);
		json.put("records", 4);
		//定义rows,存放数据
		JSONArray rows = new JSONArray();
		for(int i=0;i<4;i++)  
        {  
            // 存放一条记录的对象  
            JSONObject cell = new JSONObject();  
            cell.put("id", i);  
            if(i%2==0)  
            {  
                cell.put("userName", "polaris");  
                cell.put("gender", "女");  
            }  
            else  
            {  
                cell.put("userName", "徐新华");  
                cell.put("gender", "男");  
            }  
            cell.put("email", "polaris@gmail.com");  
            cell.put("QQ", "772"+i+"1837"+i);  
            cell.put("mobilePhone", "132"+i+"1837"+i+"3"+i);  
            cell.put("birthday", "198"+i+"-10-"+"1"+i);  
              
            // 将该记录放入rows中  
            rows.add(cell);  
        }  
          
        // 将rows放入json对象中  
        json.put("rows", rows);  
          
        // 自控制台打印输出，以检验json对象生成是否正确  
        System.out.println("要返回的json对象：\n" + json.toString());  
          
        // 设置字符编码  
        resp.setCharacterEncoding("UTF-8");  
        // 返回json对象（通过PrintWriter输出）  
        resp.getWriter().print(json);  
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
